package com.aft.terra.common.interceptor;

import java.util.Date;
import java.util.Map;

import com.aft.terra.app.Constants;
import com.aft.terra.common.utils.DateUtil;
import com.aft.terra.common.utils.RedisKit;
import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;
import com.jfinal.plugin.redis.Cache;

public class StartEndDateInterceptor implements Interceptor {

	public void intercept(Invocation invocation) {
		Controller controller = invocation.getController();
		Cache cache = RedisKit.getCache();
		Map<String,String> map = cache.get(Constants.GAME_CONFIG_PARAMS);
		DateUtil.formatStringToDate(map.get("game_start_time").toString());
		DateUtil.formatStringToDate(map.get("game_end_time").toString());
		if("1".equals(map.get("is_open_clear"))){
			String startTime = (String) map.get("clear_date_s");
			String endTime = map.get("clear_date_e");
			Long now = DateUtil.format(DateUtil.nowHHmmss(), "HH:mm:ss").getTime();
			Long st = DateUtil.format(startTime, "HH:mm:ss").getTime();
			Long nt = DateUtil.format(endTime, "HH:mm:ss").getTime();
			// 目前正在清理数据期间
			if(now > 0){
				if(now >= st){
					controller.redirect("/");
					return;
				}
			}else{
				if(now <= nt){
					controller.redirect("/");
					return;
				}
			}
		}
		
		if(new Date().getTime() < DateUtil.formatStringToDate(map.get("game_start_time").toString()).getTime()){
			controller.redirect("/");
			return;
		}
		if(new Date().getTime() > DateUtil.formatStringToDate(map.get("game_end_time").toString()).getTime()){
			controller.redirect("/");
			return;
		}
		invocation.invoke();
	}
}
